package com.zhangchuchu.neohealth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangchuchu.neohealth.entity.VisitLog;
import com.zhangchuchu.neohealth.mapper.VisitLogMapper;
import com.zhangchuchu.neohealth.service.VisitLogService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

@Service
public class VisitLogServiceImpl extends ServiceImpl<VisitLogMapper, VisitLog> implements VisitLogService {
    @Override
    public void recordVisit(HttpServletRequest request) {
        String ip = request.getRemoteAddr();
        String ua = request.getHeader("User-Agent");

        VisitLog log = new VisitLog();
        log.setIp(ip);
        log.setUserAgent(ua);
        log.setVisitTime(LocalDateTime.now());

        this.save(log);
    }

    @Override
    public List<Map<String, Object>> getLast7DaysVisits(LocalDate date) {
        LocalDate end = date;
        LocalDate start = date.minusDays(6);
        // 将 LocalDate 转换为 LocalDateTime
        LocalDateTime startDateTime = start.atStartOfDay(); // 开始日期的00:00:00
        LocalDateTime endDateTime = end.atTime(LocalTime.MAX); // 结束日期的23:59:59
        System.out.println("Start Date: " + startDateTime);
        System.out.println("End Date: " + endDateTime);
        return this.baseMapper.getVisitStats(startDateTime, endDateTime);
    }
}
